class_name Role_2_Ctrl extends Node2D
##角色2唐僧控制器
@onready var character_body_2d: CharacterBody2D = $CharacterBody2D

##动画的AimationPlayer节点
@onready var role_anima_layer: AnimationPlayer = $role_anima_layer

##玩家移动的速度
var move_speed: float = 200

var gravity:float = 980
##玩家的方向
var direction:float

var facing_direction:float

##玩家状态机（枚举值）
enum Role_State
{
	Idle,
	Run
}
##当前角色的状态
var _curr_role_state:Role_State

##角色的朝向，利用scale,x控制
@onready var move_dir: Node2D = $CharacterBody2D/Move_Dir


func _ready() -> void:
	
	pass
	
	
	
func _process(delta: float) -> void:
	update_anima()
	
	pass
	
func _physics_process(delta: float) -> void:
	var direction = Input.get_axis("A_Move","D_Move")
	
	if not is_zero_approx(direction):
		facing_direction = sign(direction)
		move_dir.scale.x = facing_direction
		
	##角色移动应用	
	character_body_2d.velocity.x = direction * move_speed
	
	if not character_body_2d.velocity.x == 0:
		_curr_role_state = Role_State.Run
		pass
	else :
		_curr_role_state = Role_State.Idle
	
	##为角色添加重力
	character_body_2d.velocity.y += gravity * delta
	character_body_2d.move_and_slide()
	pass

##更新动画
func update_anima()->void:
	match _curr_role_state:
		Role_State.Idle:
				role_anima_layer.play("Idle")
				pass
		Role_State.Run:
				role_anima_layer.play("Run")
				pass
	pass
	
